#pragma once

#include "pch.h"
#include "TbInfo.h"
#include "DBInfo.h"
#include "UserInfo.h"

struct Connection
{
	struct Desc {
		std::string m_Host{ "127.0.0.1" };
		int m_Port{ 3306 };
		std::string m_Database{ "" };
		std::string m_User{ "root" };
		std::string m_Password{ "1234" };
	} m_Desc;

	MYSQL* m_Conn{ NULL };

	Connection(const Desc& desc) :m_Desc(desc)
	{
		Init();
	}

	bool Init()
	{
		m_Conn = mysql_init(NULL);
		if (!m_Conn)
		{
			std::cout << "mysql init failed" << std::endl;
			return false;
		}

		if (!mysql_real_connect(m_Conn, m_Desc.m_Host.c_str(), m_Desc.m_User.c_str(), m_Desc.m_Password.c_str(),
			m_Desc.m_Database.c_str(), m_Desc.m_Port, NULL, 0)) {
			std::cerr << "mysql_real_m_Connect() failed: "
				<< mysql_error(m_Conn) << "\n";
			return false;
		}
	}

	void Close()
	{
		if (m_Conn)
			mysql_close(m_Conn);
	}
	~Connection()
	{
		Close();
	}
	bool Execute(const std::string& sql)
	{
		bool error = mysql_query(m_Conn, sql.c_str());

		if (error)
		{
			std::cerr << "failed error : " << mysql_error(m_Conn) << std::endl;
			std::cerr << "error code: " << mysql_errno(m_Conn) << std::endl;
		}
		return !error;
	}
	bool ListTables(std::vector<TBInfo>& infos);
	bool ListUsers(std::vector<UserInfo>& users);
	bool ListDatabaseInfos(std::vector<DatabaseInfo>& dbs);
	bool GetTableInfo(TBGetInfo& info);
	bool UseDatabase(const std::string& dbName);
};
